﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Request;
using Model.Response;
using Common;
using System.Data;
using Dapper;
namespace Dal
{
    public class UsersDAL
    {
        public ResUserInfo Login(LoginRequest model)
        {
            var dp = new DynamicParameters();
            dp.Add("@Alias", model.name);
            dp.Add("@PassWord", model.pwd);
            using (IDbConnection conn = daconn.GetConnection())
            {
                var userinfo = conn.Query<ResUserInfo>("prAPPlogin", dp, null, true, null, CommandType.StoredProcedure).SingleOrDefault<ResUserInfo>();
                return userinfo;
            }
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public bool ChangePassword(ChangePaswordRequest request)
        {

            var dp = new DynamicParameters();
            dp.Add("@UserCode", request.UserCode);
            dp.Add("@OldPassword", request.OldPassword);
            dp.Add("@NewPassword", request.NewPassword);
            dp.Add("@CoID", request.CoID);
            using (IDbConnection conn = daconn.GetConnection())
            {
                return conn.Execute("prChangePassword", dp, null, null, CommandType.StoredProcedure) > 0;
            }
        }


        /// <summary>
        /// 修改联系方式
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public bool AppAddressListUpdateSelf(AppAddressListUpdateSelfRequest request)
        {

            var dp = new DynamicParameters();
            dp.Add("@Mobile", request.Mobile);
            dp.Add("@Department", request.Department);
            dp.Add("@Duty", request.Duty);
            dp.Add("@UserCode" ,request.UserCode);
            dp.Add("@CoID", request.CoID);
            using (IDbConnection conn = daconn.GetConnection())
            {
                return conn.Execute("prAppAddressListUpdateSelf", dp, null, null, CommandType.StoredProcedure) > 0;
            }
        }

        /// <summary>
        /// 添加联系方式
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public bool AppAddressListInsert(AppAddressListInsertRequest request)
        {

            var dp = new DynamicParameters();
            dp.Add("@LinkMan",request.LinkMan);
            dp.Add("@Mobile", request.Mobile);
            dp.Add("@Department", request.Department);
            dp.Add("@Duty", request.Duty);
            dp.Add("@UserCode", request.UserCode);
            dp.Add("@CoID", request.CoID);
            using (IDbConnection conn = daconn.GetConnection())
            {
                return conn.Execute("prAppAddressListInsert", dp, null, null, CommandType.StoredProcedure) > 0;
            }
        } 
    }
}
